package com.zk.shop.controller;

import com.github.pagehelper.PageInfo;
import com.zk.shop.pojo.Manager;
import com.zk.shop.pojo.ManagerMenu;
import com.zk.shop.service.ManagerService;
import com.zk.shop.util.RespUtil;
import com.zk.shop.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 空:000000
 *
 * @param
 * @return
 */
@RequestMapping("/manager")
@RestController
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @PostMapping("/login")
    @ResponseBody
    public Result login(@Validated Manager manager, HttpServletRequest request) throws Exception {
        try {
            HttpSession session = request.getSession();
            ServletContext application = session.getServletContext();
            Manager manager1 = managerService.login(manager);
            if (null != manager1) {
                manager1.setLogin_success(1);
                Timestamp createTime = new Timestamp(new Date().getTime());
                manager1.setLastlogintime(createTime);
                manager1.setLoginarea(manager.getLoginarea());
                manager1.setLoginip(manager.getLoginip());
                Integer row = managerService.saveLoginStatus(manager1);
                if (row > 0) {
                    if ("000000".equals(manager1.getMpwd())) {
                        manager1.setMpwd("");
                        return new RespUtil().JOK("111110", "管理员您好，您的密码仍为初始密码，请立即修改密码！", true, manager1);
                    } else {
                        application.setAttribute("onlineUserMid",manager1.getMid());
                        manager1.setMpwd("");
                        return new RespUtil().JOK("111111", "查询成功，系统中存在该用户", true, manager1);
                    }
                } else {
                    return new RespUtil().MOK("000000", "更新登陆状态失败", false);
                }
            } else {
                return new RespUtil().JOK("000000", "查询成功，系统中不存在该用户", true, new Manager());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new RespUtil().MOK("000001", e.getStackTrace().toString(), false);
        }
    }

    @GetMapping("/manageTableList/{mid}/{mno}/{mrole}/{login_success}/{page}/{size}")
    @ResponseBody
    public Result manageTableList(@PathVariable("mid") String mid, @PathVariable("mno") String mno, @PathVariable("mrole") String mrole,
                                  @PathVariable("login_success") String login_success, @PathVariable("page") Integer page, @PathVariable("size") Integer size) throws Exception {
        Integer mid1 = Integer.parseInt(mid);
        Integer loginSuccess1 = managerService.getLoginStatusI(mid1);
        if (loginSuccess1 == 0) {
            return new RespUtil().MOK("10101", "登录状态更改", true);
        } else {
            Manager manager = new Manager();
            Integer login_success1 = 0;
            Integer mrole1 = 0;
            if ("null".equals(mno)) {
                mno = "%%";
            } else {
                mno = "%" + mno + "%";
            }
            manager.setMno(mno);
            if ("null".equals(mrole)) {
                mrole1 = 0;
            } else {
                mrole1 = Integer.parseInt(mrole);
            }
            manager.setMrole(mrole1);
            if ("null".equals(login_success)) {
                login_success1 = 0;
            } else {
                login_success1 = Integer.parseInt(login_success);
            }
            manager.setLogin_success(login_success1);
            List<Manager> list = managerService.manageTableList(manager, page, size);
            PageInfo<Manager> pageInfo = new PageInfo<>(list);
            return new RespUtil().POK("200", "ok", true, pageInfo);
        }
    }

    @PostMapping("/pushManager/{mid}")
    @ResponseBody
    public Result pushManager(@RequestBody Manager manager, @PathVariable("mid") Integer mid) throws Exception {
        try {
            System.out.println(mid);
            Integer mid1 = mid;
            Integer loginSuccess1 = managerService.getLoginStatusI(mid1);
            if (loginSuccess1 == 0) {
                return new RespUtil().MOK("10101", "登录状态更改", true);
            } else {
                manager.setMpwd("000000");
                manager.setLogin_success(0);
                Integer row = managerService.pushManager(manager);
                if (row > 0) {
                    return new RespUtil().MOK("200", "新增成功", true);
                } else {
                    return new RespUtil().MOK("000000", "新增失败", false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new RespUtil().MOK("000001", "/manager/pushManager接口异常:[" + e.getStackTrace() + "]", false);
        }
    }


    @PostMapping("/reset/{mid}/{mrole}")
    @ResponseBody
    public Result resetManager(@RequestBody Manager manager, @PathVariable("mid") String mid, @PathVariable("mrole") String mrole) {
        try {
            Integer mid1 = Integer.parseInt(mid);
            Integer loginSuccess1 = managerService.getLoginStatusI(mid1);
            if (loginSuccess1 == 0) {
                return new RespUtil().MOK("10101", "登录状态更改", true);
            } else {
                System.out.println(manager.toString());
                if (mid1 != manager.getMid() && manager.getMrole() == 1 && Integer.parseInt(mrole) == 1) {
                    return new RespUtil().MOK("111110", "您不能重置其他超级管理员的账户信息", true);
                } else if (mid1 != manager.getMid() && Integer.parseInt(mrole) == 0) {
                    return new RespUtil().MOK("111110", "权限不足", true);
                }
                manager.setLogin_success(0);
                manager.setMpwd("000000");
                Integer row = managerService.savePassword(manager);
                if (row > 0) {
                    return new RespUtil().MOK("200", "重置成功", true);
                } else {
                    return new RespUtil().MOK("000000", "重置失败,未找到该用户", false);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new RespUtil().MOK("000001", "/manager/reset接口异常:[" + e.getStackTrace() + "]", false);
        }
    }

    @PostMapping("/setPwd")
    @ResponseBody
    public Result setPwd(@RequestBody Manager manager) {
        try {
            Integer row = managerService.setPwd(manager);
            if (row > 0) {
                return new RespUtil().MOK("200", "修改成功", true);
            } else {
                return new RespUtil().MOK("000000", "修改失败,未找到该用户", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new RespUtil().MOK("000001", "/manager/setPwd接口异常:[" + e.getStackTrace() + "]", false);
        }
    }


    @PostMapping("/saveLoginStatus/{mid}")
    public Result saveLoginStatus(@PathVariable("mid") Integer mid) {
        try {
            Manager manager = new Manager();
            manager.setMid(mid);
            manager.setLogin_success(0);
            Integer row = managerService.saveLoginStatus(manager);
            if (row > 0) {
                return new RespUtil().MOK("200", "更新登陆状态成功", true);
            } else {
                return new RespUtil().MOK("000000", "更新失败", false);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new RespUtil().MOK("000001", "/manager/saveLoginStatus接口异常:[" + e.getStackTrace() + "]", false);
        }
    }
//
//    //删除管理员
//    @PostMapping("/remove/{mrole}")
//    @ResponseBody
//    public Result removeManager(@RequestBody Manager manager, @PathVariable("mrole") Integer mrole) {
//        try {
//            if (mrole == 0) {
//                return new RespUtil().MOK("111110", "权限不足", true);
//            } else if (mrole == 1) {
//                if (manager.getMrole() == 1) {
//                    return new RespUtil().MOK("111110", "")
//                }
//                Integer row = managerService.removeManager(mid);
//                if (row > 0) {
//                    return ;
//                }
//            }
//        }catch(Exception e){
//            return ;
//        }
//    }
    @RequestMapping("/getMenuTree/{managerid}")
    @ResponseBody
    public Result getMenuTree(@PathVariable("managerid")String managerid) throws Exception{
        System.out.println(managerid);
        try{
            List<ManagerMenu> list = managerService.getMenuTree(Integer.parseInt(managerid));
            List<ManagerMenu> list1 = new ArrayList<>();
            list.forEach(obj->{
                obj.getMm().remove(0);
                list1.add(obj);
            });
            return new RespUtil().LOK("200","菜单查询成功！",true,list1);
        }catch(Exception e){
            e.printStackTrace();
            return new RespUtil().MOK("000000","查询失败，接口异常",false);
        }
    }





}
